(function(){ 
		Ext.loadImage=function(img,callback)		
		{  			    
		    var appname = navigator.appName.toLowerCase();
		    if (appname.indexOf("netscape") == -1)//ie
		    {
		       
		        img.onreadystatechange = function () {
		            if (img.readyState == "complete")
		            {
		                callback(img);		               
		            }
		        };
		    } 
		    else //firefox
		    {		       
		        img.onload = function () {
		            if (img.complete == true)
		            {
		                callback(img);
		            }
		        }
		    }
		    return img		
		}
		
Ext.PercentLoadMask=Ext.extend(Ext.LoadMask,{    
    onLoad : function(){
        this.el.unmask(this.removeMask);
        this.el.setStyle("z-index",1000);
    },  
    onBeforeLoad : function(){
        if(!this.disabled){
            this.mask=this.el.mask("0%", this.msgCls);
        }
    },
    setMsg:function(msg)
    {
       if(!this.disabled){           
           this.el.select(".ext-el-mask-msg").first().first().update(msg);
       }
    }

})	
		
function Handler(demo)
{
  return {
	swfUploadLoaded : function() {
		//demo.swfStart(this); // This refers to the SWFObject because		
		// init button here
	},
	fileQueued : function(file) {
		try {		
			 demo.file=file;
			 this.startUpload(file.id);
			 demo.mask.show();
			 demo.mask.setMsg("0%");
			//{id:file.id,fileName:file.name,fileSize:file.size};					
		} catch (ex) {
			alert(ex+"start error");
		}
	},
	
	fileQueueError : function(file, errorCode, message) {
		try {
			var errorName = "";
			switch (errorCode) {
				case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED :
					errorName = "上文件个数超过限制";
					break;
				case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT :
					errorName = "文件大小超过限制";
					break;
				case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE :
					errorName = "不能上传零字节文件";
					break;
				case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE :
					errorName = "无效文件类型";
					break;
				default :
					errorName = "为止错误";
					break;
			}

			var errorString = errorName
					+ ":File ID: "
					+ (typeof(file) === "object" && file !== null
							? file.id
							: "na") + ":" + message;

			alert(errorString);

		} catch (ex) {
			this.debug(ex);
		}
	},
	uploadProgress : function(file, bytesLoaded, totalBytes) {
		try {
			var percent = Math.ceil((bytesLoaded / file.size) * 100);	
			demo.mask.setMsg(percent+"%");
		} catch (ex) {
			alert(ex);
		}
	},
	uploadSuccess : function(file, serverData, receivedResponse) {
		try 
	    {		
			if(serverData)
			{	    	  
	    	  var data=Ext.decode(serverData.trim());	
	    	  if(data.ok)
	    	  {
	    	    demo.mask.hide();
	    	    //demo.loadImage("image/"+data.id);
	    	    demo.fireEvent("imageupload",data);
	    	  }
	    	  else
	    	  {
	    	    alert("上传出错");
	    	  }	    	  
			}		 
			
		} catch (ex) {
			alert(ex);
		}
	},
	uploadError : function(file, errorCode, message) {		
		try {
			var errorName = "";
			switch (errorCode) {
				case SWFUpload.UPLOAD_ERROR.HTTP_ERROR :
					errorName = "HTTP ERROR";
					break;
				case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL :
					errorName = "MISSING UPLOAD URL";
					break;
				case SWFUpload.UPLOAD_ERROR.IO_ERROR :
					errorName = "IO ERROR";
					break;
				case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR :
					errorName = "SECURITY ERROR";
					break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED :
					errorName = "UPLOAD LIMIT EXCEEDED";
					break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED :
					errorName = "UPLOAD FAILED";
					break;
				case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND :
					errorName = "SPECIFIED FILE ID NOT FOUND";
					break;
				case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED :
					errorName = "FILE VALIDATION FAILED";
					break;
				case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED :
					errorName = "FILE CANCELLED";
					break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED :
					errorName = "FILE STOPPED";
					return;					
				default :
					errorName = "UNKNOWN";
					break;
			}

			alert(errorString);

		} catch (ex) {
			this.debug(ex);
		}
	}
};
  
}// end handler define

		
		
  Ext.ns("Ext.ux.ImageField");
  Ext.ux.ImageField=function(config)
  {
     config.autoEl={
     tag:"div",
     style:'position:relative'   
     };
     Ext.ux.ImageField.superclass.constructor.call(this,config);      
     this.ttpl=[
         "<span id='swfbt'></span>"
     ].join("");
     
     this.addEvents("imageload");
     this.addEvents("imageupload");
     
  }  
  Ext.extend(Ext.ux.ImageField,Ext.BoxComponent,{
     onRender : function(ct, position){
        Ext.BoxComponent.superclass.onRender.call(this, ct, position);       
        this.bt=this.el.createChild(this.ttpl);            
        var image =new Image();        
        this.el.appendChild(image);	         
        this.image=image;// this is a dom
        this.mask=new Ext.PercentLoadMask(Ext.getBody());
        this.initswfupload();     
        this.loadDefaultImage();  
           
     },
     loadDefaultImage:function(){
       this.loadImage(this.swfuploadBase+"/default.jpg"); 
     },
     loadImage:function(url){       
        var image=new Image();
        Ext.get(this.image).remove();   
        this.el.appendChild(image);             
        this.image=image;
        var me=this;        
        this.mask.show();   
        this.mask.setMsg("loading....")    
        Ext.loadImage(image,function(img){  	  
            me.mask.hide();     
            var vs=Ext.getDoc().getViewSize();
	        var width=me.el.parent().getWidth()+0.000000001;	
	        var ow=img.width;   
	        var oh=img.height
	        img.width=width-5;	 
	        if(img.height>=vs.height-200)
	        {
	           img.height=vs.height-200;
	           img.width=img.height*ow/oh-5;           
	        }
	        else
	        {
	        	if(Ext.isIE)
		        {
		          img.height=img.height*width/ow;//fix for ie
		          
		        }
	        }
	        me.el.setHeight(img.height+me.bt.getHeight());//sync el height   
	        me.fireEvent("imageload",me);
        });
        image.src=url;            
     },
     initswfupload:function()
     {
        var swfHander=new Handler(this);
		var swfupload = new SWFUpload({
				upload_url : this.uploadUrl,
				flash_url : this.swfuploadBase+"/swfupload.swf",
				button_placeholder_id : "swfbt",
				button_image_url : this.swfuploadBase+"/bt.png",
				button_text_right_padding : 100,				
				button_width: 61,
				button_height : 22,				
				button_action : SWFUpload.BUTTON_ACTION.SELECT_FILE,
				file_types: "*.jpg; *.jpeg; *.psd; *.png; *.gif",				
				// handler here 
				swfupload_loaded_handler : swfHander.swfUploadLoaded,				
				file_queued_handler : swfHander.fileQueued,
				file_queue_error_handler : swfHander.fileQueueError,	
				upload_progress_handler : swfHander.uploadProgress,
				upload_error_handler : swfHander.uploadError,
				upload_success_handler : swfHander.uploadSuccess				
		});
        this.swfupload=swfupload;             
     },
	    beforeDestroy : function(){    
	        Ext.Container.superclass.beforeDestroy.call(this);	        
	        this.swfupload.destroy();
	        Ext.destroy(this.el);
	    }    
  });
  
  Ext.reg("imagefield",Ext.ux.ImageField);

})();